package com.app.extended_plugin;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** ExtendedPlugin */
public class ExtendedPlugin implements FlutterPlugin,ActivityAware,MethodCallHandler {
  private static final String keyboardEventChannelName = "keyboard_visibility_event";
  private static final String screenShotChannelName = "screen_shot_event";
  private static final String networkChannelName = "network_event";
  private static final String TAG = "ExtendedPlugin";
//  private KeyboardEventHandler keyboardHandler;
  private List<ActivityAware> needActivityAware = new ArrayList<>();
  private Activity mActivity;
  private BinaryMessenger mBinaryMessenger;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    Log.i(TAG, "onAttachedToEngine: >>>>>>>>>>》》》》》》》》>>>>");
    onAttachedToEngine(flutterPluginBinding.getApplicationContext(),flutterPluginBinding.getBinaryMessenger());
  }
  private void onAttachedToEngine(Context applicationContext, BinaryMessenger messenger) {
    Log.i(TAG, "onAttachedToEngine: ");
    this.mBinaryMessenger = messenger;
  }

  /**
   * 加载activity的回调
   */
  private void onActivityAttached(){
    //初始化事件监听
    MethodChannel channel = new MethodChannel(mBinaryMessenger, "extended_plugin");
    channel.setMethodCallHandler(new ExtendedPlugin());
    //-----------------键盘------------------
    EventChannel eventChannel = new EventChannel(mBinaryMessenger, keyboardEventChannelName);
    KeyboardEventHandler keyboardHandler = new KeyboardEventHandler(mActivity);
    eventChannel.setStreamHandler(keyboardHandler);
    //-----------------屏幕截图------------------
    EventChannel screenChannel = new EventChannel(mBinaryMessenger, screenShotChannelName);
    ScreenEventHandler screenEventHandler = new ScreenEventHandler(mActivity);
    screenChannel.setStreamHandler(screenEventHandler);
    //-----------------net work ------------------
    EventChannel networkChannel = new EventChannel(mBinaryMessenger, networkChannelName);
    NetworkEventHandler networkEventHandler = new NetworkEventHandler(mActivity);
    networkChannel.setStreamHandler(networkEventHandler);

    MethodChannel displayChannel = new MethodChannel(mBinaryMessenger, "plugin_channel/pixel");
    displayChannel.setMethodCallHandler(new MetricsDisplayHandler(mActivity));


    //-----------------添加到集合统一回调生命周期-----------------
  }

  private void addActivityAware(ActivityAware activityAware){
    needActivityAware.add(activityAware);
  }
  // This static function is optional and equivalent to onAttachedToEngine. It supports the old
  // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
  // plugin registration via this function while apps migrate to use the new Android APIs
  // post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
  //
  // It is encouraged to share logic between onAttachedToEngine and registerWith to keep
  // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
  // depending on the user's project. onAttachedToEngine or registerWith must both be defined
  // in the same class.
  public static void registerWith(Registrar registrar) {
    final ExtendedPlugin plugin = new ExtendedPlugin();
    plugin.mActivity = registrar.activity();
    plugin.onAttachedToEngine(registrar.context(),registrar.messenger());
    plugin.onActivityAttached();
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else {
      result.notImplemented();
    }
  }



  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    Log.i(TAG, "onDetachedFromEngine: ");
    needActivityAware.clear();
    mActivity = null;
    mBinaryMessenger = null;

  }

  @Override
  public void onAttachedToActivity(ActivityPluginBinding binding) {
    Log.i(TAG, "onAttachedToActivity: ");
    mActivity = binding.getActivity();
    onActivityAttached();
    for(ActivityAware aware:needActivityAware){
      aware.onAttachedToActivity(binding);
    }

  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {
    Log.i(TAG, "onDetachedFromActivityForConfigChanges: ");

    for(ActivityAware aware:needActivityAware){
      aware.onDetachedFromActivityForConfigChanges();
    }
  }

  @Override
  public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
    Log.i(TAG, "onReattachedToActivityForConfigChanges: ");
    mActivity = binding.getActivity();
    onActivityAttached();

    for(ActivityAware aware:needActivityAware){
      aware.onReattachedToActivityForConfigChanges(binding);
    }
  }

  @Override
  public void onDetachedFromActivity() {
    Log.i(TAG, "onDetachedFromActivity: ");
    for(ActivityAware aware:needActivityAware){
      aware.onDetachedFromActivity();
    }
  }
}

